/**
 * Created by hda on 2017/5/22.
 */
define([], function () {
    var model = {};
    model.run = function (obj, rule) {
        //表单对象，验证规则

        var obj = {
            title: '',
            price: 1
        }
        var rule = {
            price: {
                require_error: '',
                verify_error: '',
                rules: 'required|regex',// 身份证   电话  邮箱
                regex: /^[0-9]+$/

            },
            title: {
                require_error: '',
                verify_error: '',
                rules: 'required',// 身份证   电话  邮箱
                // range:',6'
            }
        }

        //循环rule
        for (var m in rule) {
            var flag = model.do(obj[m], rule[m]['rules'], rule[m]['regex']);
            if (!flag) {
                console.log('不通过不通过')
                return false;
            } else {
                console.log('测试通过')
            }
        }


    }

    model.regex = {
        // required: /^\s*$/,
        required: /^[^\s]+$/,
        number: /^[0-9]+$/,
        int: /^\-?[0-9]+$/,
        decimal: /^\-?[0-9]*\.?[0-9]+$/,
        email: /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/
    }

    model.do = function (val, rules, regex) {
        var arr = rules.split('|');
        for (var i = 0; i < arr.length; i++) {
            var regex_now;
            var rule = arr[i];
            if (typeof(model.regex[rule]) != 'undefined') {
                regex_now = model.regex[rule];
            }
            if (rule == 'regex') {
                regex_now = regex;
            }
            var re = new RegExp(regex_now);
            if (!re.test(val)) {
                return false;
            }
        }
        return true;
    }

    return model;
});